package com.mocha.bass.test.jiangan;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.hyzh.latte.NezhaApplication;
import com.hyzh.latte.util.JsonUtil;
import com.hyzh.latte.vehicle.dmo.PointBO;
import com.hyzh.latte.vehicle.dmo.entity.PointPGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePGEntity;
import com.hyzh.latte.vehicle.dmo.entity.PolePointPGEntity;
import com.hyzh.latte.vehicle.repository.CameraPGRepository;
import com.hyzh.latte.vehicle.repository.PointPGRepository;
import com.hyzh.latte.vehicle.repository.PolePGRepository;
import com.hyzh.latte.vehicle.repository.PolePointPGRepository;
import lombok.extern.slf4j.Slf4j;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author songkui
 * @since 2024/3/29 23:36
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = NezhaApplication.class)
@Slf4j
public class PoleUpdate {

    @Autowired
    private PolePGRepository polePGRepository;
    @Autowired
    private PointPGRepository pointPGRepository;

    @Test
    public void test(){
        List<PointPGEntity> pointList = pointPGRepository.lambdaQuery().eq(PointPGEntity::getParkCode, "jiangan").list();
        Map<String, PointPGEntity> pointMap = pointList.stream().collect(Collectors.toMap(PointPGEntity::getPointNo, Function.identity()));

        List<PolePGEntity> list = polePGRepository.lambdaQuery().eq(PolePGEntity::getParkCode, "jiangan").list();
        for (PolePGEntity entity : list){
            PointPGEntity xpoint = pointMap.get(entity.getXNo());
            PointPGEntity ypoint = pointMap.get(entity.getYNo());
            if (null == xpoint && null == ypoint){
                log.error("pole point is null pole {};  xno {}  yno{}", entity.getPoleCode(), entity.getXNo(), entity.getYNo());
            }else {
                PolePGEntity ne = new PolePGEntity();
                ne.setId(entity.getId());
                if (null != xpoint){
                    ne.setXLat(xpoint.getLat());
                    ne.setXLon(xpoint.getLon());
                    ne.setXHeight(xpoint.getHeight());
                }
                if (null != ypoint){
                    ne.setYLat(ypoint.getLat());
                    ne.setYLon(ypoint.getLon());
                    ne.setXHeight(ypoint.getHeight());
                }
                polePGRepository.updateById(ne);
            }

        }



    }



}
